<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理| {:config('app_name')}</title>
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css">
    <link rel="stylesheet" href="__PUBLIC__/plugins/font-awesome-4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="__PUBLIC__/css/style.css">
    <script type="text/javascript" src="__PUBLIC__/js/jquery.min.js"></script>
    {block name="style"}{/block}
    <style type="text/css">
        .layui-layer-prompt .layui-layer-btn1{background: red !important; border: none!important; color: #FFF!important;}
    </style>
</head>

<body class="layui-layout-body">
    {php}$isClose=isset($home_index)&&$home_index;{/php}
    <div class="layui-layout layui-layout-admin" layui-layout="{$isClose?'closed':''}">
        <div class="layui-header">
            <div class="layui-logo">
                <span>{:config('app_name')} 管理系统</span>
            </div>
            <!-- 头部区域 -->
            <ul class="layui-nav layui-layout-left">
                {php}if(!$isClose):{/php}
                <li class="layui-nav-item layadmin-flexible" lay-unselect>
                    <a href="{:url('index/flexible')}" class="ajax-flexible" title="侧边伸缩">
                        <i class="layui-icon layui-icon-shrink-right"></i>
                    </a>
                </li>
                {php}endif;{/php}
                <!--
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:;" id="refresh" title="刷新数据">
                    <i class="layui-icon layui-icon-refresh"></i>
                    </a>
                </li>
                -->
            </ul>
            <ul class="layui-nav layui-layout-left" style="margin-left: 60px;">
                {php}$topNAV = app()->rbac->getMenu();{/php}
                {foreach name="topNAV.menu" id="top"}
                <li class="layui-nav-item"><a href="{:url($top.name, ['menu_id'=>$top.id, 'is_top' => 1])}">{$top.title}</a></li>
                {/foreach}
            </ul>
            <ul class="layui-nav  layui-layout-right">
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:" class="user"><img src="{:app()->user->face}" class="layui-nav-img">{:app()->user->nickname} <i class="layui-icon layui-icon-more-vertical"></i></a>
                    <dl class="layui-nav-child">
                        <dd><a href="{:url('info/user')}"><i class="fa fa-user"></i> 个人信息</a></dd>
                        <dd id="change-warehouse"><a href="javascript:void(0)"><i class="fa fa-home"></i> 切换仓库</a></dd>
                        <hr>
                        {php}if(app()->rbac->isRole('super admin')):{/php}
                            <dd><a href="{:url('info/clearCache')}"><i class="fa fa-trash-o"></i> 清除缓存</a></dd>
                            <hr>
                        {php}endif;{/php}
                        <dd><a href="{:url('public/logout')}"><i class="fa fa-sign-out"></i> 退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
        {php}$__NAV__ = app()->rbac->getMenu(null, null, 1);{/php}
        {include file="public/menu" /}
        <div class="main layui-body">
            <div class="main-header">
                <div class="layui-breadcrumb">
                    <a href="{:url('/')}"><i class="fa fa-dashboard"></i> 控制台</a>
                    {volist name="__NAV__.crumb" id="vo"}
                        <a href="{$vo.name|url}"><i class=<i class="fa {$vo.icon}"></i> {$vo.title}</a>
                    {/volist}
                </div>
            </div>
            <div class="main-content">
                {block name="body"}
                    <div class="layui-fluid" style="padding: 0 12px;">
                        <div class="layui-card">
                            <div class="layui-card-header">{$__NAV__.self.title}</div>
                            <div class="layui-card-body">
                                {block name="content"}{/block}
                            </div>
                        </div>
                    </div>
                {/block}
            </div>
            <div class="main-footer">
                <!-- 底部固定区域 -->
                Copyright © 2020-{:date('Y')} {:config('app_name')} 后台管理系统. All rights reserved.
            </div>
        </div>
        <script type="text/html" id="change-warehouse-html">
            <div class="layui-form box-body">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库</label>
                    <div class="layui-input-block">
                        <select id="change-warehouse-select" lay-search lay-filter="change-warehouse">
                            {php}$changeWarehouse = \app\common\model\Warehouse::getAll();{/php}
                            {foreach $changeWarehouse as $w}
                            <option {if $curr_warehouse.warehouse_id==$w.warehouse_id}selected{/if} value="{$w.warehouse_id}">{$w.name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
            </div>
        </script>
    </div>
<script type="text/javascript" src="__PUBLIC__/layui/layui.js"></script>
<script type="text/javascript">
/**
 * 配置全局常量
 */
var DOMAIN_URL = "{$domain_url}";
layui.config({
    base: '__PUBLIC__/js/'
}).use('longli');
// 读取消息并打开
$.get("{:url('/public/message')}").done(function(html)
{
    if(html.length > 0)
    {
        if(html.substr(0, 3) != "<ul") return false;
        layer.open({
            type: 1,
            shade: 0.7,
            time: 20000,
            area: ['60%', '60%'],
            content: html
        });
    }
});

$("#change-warehouse").click(function()
{
    parent.layer.open({
        type: 1,
        title: '切换仓库',
        content: $("#change-warehouse-html").html(),
        scrollbar: false,
        area: ['30%', '50%'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var wid = $("#change-warehouse-select option:selected").val();
            layer.close(index);
            if(!wid) return false;
            $.get("{:url('public/change_warehouse')}", {warehouse_id: wid}).done(function(response)
            {
                var icon = response.code == 1 ? 6 : 5;
                layer.msg(response.msg, {icon: icon, time: 1500}, function(){location.reload()});
            });
        },
        success: function(layero, index)
        {
            layui.form.render('select');
        },
    }, 'html');
});

// 重置表格大小
function resetTableSize(table, id)
{
    if(!table || typeof table.resize !== "function") return;
    $(".ajax-flexible").click(function()
    {
        setTimeout(function()
        {
            table.resize(id);
        }, 300);
    });
}

/**
 * 获取选种的 id
 * @returns {[]}
 */
function getIds(classes)
{
    var ids = [];
    $(classes + ":checked").each((i, e) => ids.push($(e).val()));
    return ids;
}

/**
 * 单个审批刷新页面
 */
$(document).on("click", ".check-job-flow-reload", function()
{
    var $this = $(this)
        , item = $this.attr('item');
    if(!item) return false;
    checkFlow($(this).attr("item"), function(response)
    {
        if(response.code == 1) location.reload();
    });
});

/**
 * 单个审批
 */
$(document).on("click", ".check-job-flow", function()
{
    var $this = $(this)
        , item = $this.attr('item');
    if(!item) return false;
    checkFlow(item, function(response)
    {
        if(response.code == 1) $(".data").closest('.data-list').getList();
    });
});
/**
 * 批量审批
 */
$("#batch-check").click(function()
{
    var $this = $(this)
        , target = $this.attr('target')
        , msg = $this.attr('msg') || '请选择要审批的工作流';
    if(!target) return false;
    var ids = getIds('.' + target);
    if(ids.length <  1)
    {
        layer.msg(msg);
        return false;
    }
    checkFlow(ids.join(","), function(response)
    {
        if(response.code == 1) $(".data").closest('.data-list').getList();
    });
});

/**
 * 审批工作流
 * @param targetId 采购id, 多个使用英文逗号隔开
 */
function checkFlow(targetId, fn)
{
    if($.type(targetId) == "array") targetId = targetId.join(",");
    var statusPass = "{$check_status_pass}"
        , statusReject = "{$check_status_reject}";
    layer.prompt({
        btn: ['通过', '驳回', '取消']
        , title: '审批'
        , formType: 2
        , value: '同意'
        , btn2: function(index, layero)
        {
            layer.load(0, {shade: false});
            var text = layui.jquery('#layui-layer' + index + " .layui-layer-input").val();
            if(!text || text == '同意') text = '驳回';
            $.post("{:url('check')}", {target_id:targetId, status:statusReject, remark: text}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = response.code == 1 ? 6 : 5;
                layer.msg(response.msg, {icon: icon, time: 1500});
                if(typeof fn === "function") fn(response);
            }).error(function(xhr)
            {
                layer.msg('服务器异常，请稍后重试~', {icon: 5});
                layer.closeAll('loading');
            });
        }
        , btn3: function(index, layero)
        {
            layer.close(index);
        }
    }, function(text, index)
    {
        layer.close(index);
        layer.load(0, {shade: false});
        $.post("{:url('check')}", {target_id:targetId, status: statusPass, remark: text}).done(function(response)
        {
            layer.closeAll('loading');
            var icon = response.code == 1 ? 6 : 5;
            layer.msg(response.msg, {icon: icon, time: 1500});
            if(typeof fn === "function") fn(response);
        }).error(function(xhr)
        {
            layer.msg('服务器异常，请稍后重试~', {icon: 5});
            layer.closeAll('loading');
        });
    });
}
</script>
{block name="script"}{/block}
</body>
</html>